package com.sjyy.common.util;

import java.util.ArrayList;
import java.util.List;

import com.sjyy.common.authority.enums.AuthorityTypeEnum;

public class AuthorityUtil
{
	/**
	 * 判断是否有权限
	 * 
	 * @param akey
	 * @param session
	 * @return
	 */
	public static boolean hasAuthority( int akey, String[] rc )
	{
		if (rc == null || rc.length == 0 || rc.length < akey)
		{
			return false;
		}

		String value = rc[akey];
		if (value.equals("1"))
		{
			return true;
		}

		return false;
	}
	
	/**
	 * 判断是否有权限
	 * 
	 * @param akey
	 * @param session
	 * @return
	 */
	public static boolean hasAuthority( int akey, String rc )
	{
		if (rc == null || rc.length() == 0 || rc.length() < akey)
		{
			return false;
		}

		String value = String.valueOf(rc.charAt(akey));
		if (value.equals("1"))
		{
			return true;
		}

		return false;
	}

	/**
	 * 创建权限字符串
	 * 
	 * @param akeys
	 *            有权限的项,比如 1,3,6,11,20
	 * @return 权限字符串, 比如0101001001000000000
	 */
	public static String makeAuthorityString( String akeys )
	{
		StringBuilder sb = new StringBuilder(_RAWSTR);
		String[] akeys_s = akeys.split(",");
		for (String akey : akeys_s)
		{
			if (null == akey || "".equals(akey))
			{
				continue;
			}
			Integer ak = Integer.parseInt(akey);
			sb.setCharAt(ak, '1');
		}

		return sb.toString();
	}
	
	/**
	 * 创建权限字符串
	 * 
	 * @param akeys
	 *            有权限的项,比如 1,3,6,11,20
	 * @return 权限字符串, 比如0101001001000000000
	 */
	public static String makeAuthorityQuery( String akeys )
	{
		StringBuilder sb = new StringBuilder(_RAW);
		String[] akeys_s = akeys.split(",");
		for (String akey : akeys_s)
		{
			if (null == akey || "".equals(akey))
			{
				continue;
			}
			Integer ak = Integer.parseInt(akey);
			sb.setCharAt(ak, '1');
		}

		return sb.toString();
	}

	/**
	 * 创建权限字符串
	 */
	public static String makeAuthorityQuery( Integer akeys ){
		return makeAuthorityQuery(new Integer[]{akeys});
	}
	
	/**
	 * 创建权限字符串
	 */
	public static String makeAuthorityQuery( Integer[] akeys ){
		StringBuilder sb = new StringBuilder(_RAW);
		for (Integer akey : akeys){
			if (null == akey || "".equals(akey)){
				continue;
			}
			sb.setCharAt(akey, '1');
		}
		return sb.toString();
	}

	/**
	 * 创建权限字符串
	 */
	public static String makeAuthorityQuery( AuthorityTypeEnum akeys ){
		return makeAuthorityQuery(new AuthorityTypeEnum[]{akeys});		
	}
	
	/**
	 * 创建权限字符串
	 */
	public static String makeAuthorityQuery( AuthorityTypeEnum[] akeys ){
		StringBuilder sb = new StringBuilder(_RAW);
		for (AuthorityTypeEnum akey : akeys){
			if (null == akey || "".equals(akey)){
				continue;
			}
			sb.setCharAt(akey.getIndex(), '1');
		}
		return sb.toString();
	}
	
	/**
	 * 创建权限
	 * 
	 * @param 权限字符串, 比如0101001001000000000
	 *            
	 * @return List<AuthorityTypeEnum>
	 */
	public static List<AuthorityTypeEnum> makeAuthoritys( String properties ){
		List<AuthorityTypeEnum> authoritys = new ArrayList<AuthorityTypeEnum>();
		char[] chars = properties.toCharArray();
		for (int i = 0; i < chars.length; i++){
			if("1".equals(String.valueOf(chars[i]))){
				authoritys.add(AuthorityTypeEnum.getAuthorityType(i));
			}
		}
		return authoritys;
	}

	final public static int AUTH_LENGTH = AuthorityTypeEnum.values().length;
	
	final public static String _RAW_FULL = "__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________";
	final public static String _RAWSTR_FULL = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
	final public static String _RAW = _RAW_FULL.substring(0,AUTH_LENGTH)+"%";
	final public static String _RAWSTR = _RAWSTR_FULL.substring(0,AUTH_LENGTH);
}
